import {AxdrType} from "../asn1.axdr/AxdrType";
import {ReverseByteArrayInputStream} from "../ReverseByteArrayInputStream";
import {ReverseByteArrayOutputStream} from "../ReverseByteArrayOutputStream";
import {AxdrSequenceOf} from "../asn1.axdr/AxdrSequenceOf";
import {A_RecordRow} from "./A_RecordRow";
import {Buffer} from "buffer";
import {AxdrEnum} from "../asn1.axdr/AxdrEnum";
import {OAD} from "./OAD";
import {RCSD} from "./RCSD";


enum Choice {
    _ERR_NONE_SELECTED = -1,
    DAR = 0,
    RECORDS = 1,
}


export class SubSeqOfRecords extends AxdrSequenceOf<A_RecordRow>{

    dataLength : number;

    createListElement(): A_RecordRow {
        let record = new A_RecordRow();
        record.set_length(this.dataLength)
        return record;
    }

    constructor() {
        super();
    }

    set_dataLength(dataLength: number) {
        this.dataLength = dataLength
    }

    encode(output: ReverseByteArrayOutputStream): number {
        return super.encode(output);
    }

    decode(input: ReverseByteArrayInputStream): number {
        return super.decode(input);
    }

}


export class SubChoiceRespData implements AxdrType{

    dataCode : Buffer | null = null;
    choice : Choice = Choice._ERR_NONE_SELECTED;
    length : number = 0;
    dar : AxdrEnum | null = null;
    records : SubSeqOfRecords | null = null;

    constructor() {
    }

    set_length(length : number){
        this.length = length;
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    setDar(dar : AxdrEnum){
        this.resetChoices();
        this.choice = Choice.DAR;
        this.dar = dar;
    }

    setRecords(records : SubSeqOfRecords){
        this.resetChoices();
        this.choice = Choice.RECORDS;
        this.records = records;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;
        let choosen = new AxdrEnum();
        choosen.set_const();

        codeLength += choosen.decode(input);
        this.resetChoices();
        this.choice = choosen.getValue();

        if (this.choice == Choice.DAR) {
            this.dar = new AxdrEnum();
            this.dar.set_const();
            codeLength += this.dar.decode(input);
            return codeLength;
        }else if (this.choice == Choice.RECORDS) {
            this.records = new SubSeqOfRecords();
            this.records.set_dataLength(this.length);
            codeLength += this.records.decode(input);
            return codeLength;
        }
        throw new Error("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    encode(output: ReverseByteArrayOutputStream): number {
        if (this.dataCode != null) {
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
            return this.dataCode.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Error("Error encoding AxdrChoice: No item in choice was selected.");
        }

        let codeLength = 0;

        if (this.choice == Choice.RECORDS) {
            if(this.records != null){
                codeLength += this.records.encode(output);
                let c = new AxdrEnum();
                c.set_min_max_val(1);
                codeLength += c.encode(output);
                return codeLength;
            }else{
                throw new Error("records is null");
            }
        }else if (this.choice == Choice.DAR) {
            codeLength += this.dar.encode(output);
            let c = new AxdrEnum();
            c.set_min_max_val(0);
            codeLength += c.encode(output);
            return codeLength;
        }

        throw new Error("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    getChoiceIndex(): Choice {
        return this.choice;
    }

    resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.dar = null;
        this.records = null;
    }

    toString(): string {
        if (this.choice == Choice.DAR) {
            if (this.dar != null) {
                return "choice: {dar: " + this.dar + "}";
            } else {
                return "choice is dar but dar is null";
            }
        }else if(this.choice == Choice.RECORDS){
            if(this.records != null){
                return "choice: {records: " + this.records + "}";
            }else{
                return "choice is records but records is null";
            }
        } else {
            return "unknown";
        }
    }

}


export class A_ResultRecord implements AxdrType{

    dataCode : Buffer | null = null;
    oad : OAD | null = null;
    rcsd : RCSD | null = null;
    respData : SubChoiceRespData | null = null;

    constructor() {
    }

    set_dataCode(dataCode : Buffer){
        this.dataCode = dataCode;
    }

    set_all(oad : OAD, rcsd : RCSD, respData : SubChoiceRespData){
        this.oad = oad;
        this.rcsd = rcsd;
        this.respData = respData;
    }

    decode(input: ReverseByteArrayInputStream): number {
        let codeLength = 0;

        this.oad = new OAD();
        codeLength += this.oad.decode(input);

        this.rcsd = new RCSD();
        codeLength += this.rcsd.decode(input);

        this.respData = new SubChoiceRespData();
        this.respData.set_length(this.rcsd.size());
        codeLength += this.respData.decode(input);

        return codeLength;
    }

    encode(output: ReverseByteArrayOutputStream): number {
        let codeLength;
        if (this.dataCode != null) {
            codeLength = this.dataCode.length;
            for (let i = this.dataCode.length - 1; i >= 0; i--) {
                output.write(this.dataCode[i]);
            }
        } else {
            if(this.respData != null && this.rcsd != null && this.oad != null){
                codeLength = 0;
                codeLength += this.respData.encode(output);
                codeLength += this.rcsd.encode(output);
                codeLength += this.oad.encode(output);
            }else{
                throw new Error("respData || rcsd || oad is null");
            }
        }
        return codeLength;
    }

    encodeAndSave(encodingSizeGuess: number) {
        let revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        this.encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    toString(): string {
        if (this.oad != null && this.rcsd != null && this.respData != null) {
            return "sequence: {" + "oad: " + this.oad + ", rcsd: " + this.rcsd + ", respData: " + this.respData + "}";
        } else {
            return "respData || rcsd || oad is null";
        }
    }

}
